package com.cqu.lpss.controller;

import com.cqu.lpss.pojo.PageBean;
import com.cqu.lpss.pojo.Result;
import com.cqu.lpss.pojo.Textinfo;
import com.cqu.lpss.service.impl.TextInfoImpl;
import com.cqu.lpss.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/textinfo")
public class TextInfoController {
    @Autowired
    TextInfoImpl textInfoImpl;
    @Autowired
    AliOSSUtils aliOSSUtils;
    @PostMapping
    public Result list(@RequestParam(defaultValue = "1")int page,
                       @RequestParam(defaultValue = "10")int pageSize,
                       String textTitle,
                       String textUrl){
        PageBean<Textinfo> pageBean = textInfoImpl.list(page, pageSize, textTitle, textUrl);
        return Result.success(pageBean);
    }
    @PostMapping("/upload")
    public Result upload(@RequestPart(value = "file") MultipartFile uploadfile) throws IOException {
        log.info("文件：{}",uploadfile);
        log.info("文件上传，文件名：{}", uploadfile.getOriginalFilename());
        //阿里云工具类进行文件上传
        String url = aliOSSUtils.upload(uploadfile);
        log.info("文件上传完成，文件访问的url:{}",url);
        return Result.success(url);
    }
    @PostMapping("/submit")
    public Result submit(@RequestBody Textinfo textinfo){
        int ans = textInfoImpl.insert(textinfo);
        if(ans==1){
            return Result.success();
        }else {
            return Result.erro("InsertErro");
        }
    }
    @PostMapping("/all")
    public Result listall(){
        List<Textinfo> listall = textInfoImpl.listall();
        return Result.success(listall);
    }
}
